/**
 * lilang
 * Check.java
 * 2013-8-14下午2:49:20
 */
package com.cq.oa.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author lilang
 * 
 */
public class PictureCheckCode extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public PictureCheckCode() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void init() throws ServletException {
		super.init();
	}

	/* 该方法主要作用是获得随机生成的颜色 */
	public Color getRandColor(int s, int e) {
		Random random = new Random();
		if (s > 255)
			s = 255;
		if (e > 255)
			e = 255;
		int r, g, b;
		r = s + random.nextInt(e - s); // 随机生成RGB颜色中的r值
		g = s + random.nextInt(e - s); // 随机生成RGB颜色中的g值
		b = s + random.nextInt(e - s); // 随机生成RGB颜色中的b值
		return new Color(r, g, b);
	}

	
	
	
	@Override
	public void service(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
		this.creatRand(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
		this.creatRand(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
		this.creatRand(request, response);
	}
	/**
	 * 旋转和缩放文字
     * 必须要使用Graphics2d类
	 * @param request
	 * @param response
	 * @throws IOException
	 * lilang 
	 * 2013-8-14下午3:09:39
	 */
	private void creatRand(HttpServletRequest request, HttpServletResponse response) throws IOException{
			// 设置不缓存图片
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "No-cache");
			response.setDateHeader("Expires", 0);
			// 指定生成的响应图片,一定不能缺少这句话,否则错误.
			response.setContentType("image/jpeg");
		   int width=80;
	       int height=28;
	       BufferedImage img = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
	       Graphics g = img.getGraphics();
	       Graphics2D g2d = (Graphics2D) g;
	       g2d.setFont(new Font("黑体",Font.BOLD,17));
	       StringBuffer sb = new StringBuffer();
	       for(int i=0;i<4;i++){
	    	   String tmp = CreatRandUtils.getRands();
	           g2d.drawString(tmp,i*18,height-5);
	           sb.append(tmp);
	       }
	       g2d.dispose();
	       HttpSession session = request.getSession(true);
		   session.setAttribute("checkRand", sb.toString());
	       ImageIO.write(img, "JPEG",response.getOutputStream());
	}
	
	
	
	public static void main(String[] args) throws IOException {
		  new PictureCheckCode().creatRand(null, null);
	}
}
